﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbSysMenuDal
	{
		public DbSysMenuDal()
		{
		}
 		#region Insert
 		public static int Insert(DbSysMenu dbSysMenu)
		{	
			string sql = @"Insert Into DbSysMenu (
					SmName,
					SmIcon,
					SmCode,
					SmUrl,
					SmStatus,
					SmUpCode,
					SmOrder,
					SmLevel
				) values('{0}','{1}','{2}','{3}',{4},'{5}',{6},{7})";
			sql = string.Format(sql,
					SqlHelper.String(dbSysMenu.SmName),
					SqlHelper.String(dbSysMenu.SmIcon),
					SqlHelper.String(dbSysMenu.SmCode),
					SqlHelper.String(dbSysMenu.SmUrl),
					dbSysMenu.SmStatus,
					SqlHelper.String(dbSysMenu.SmUpCode),
					dbSysMenu.SmOrder,
					dbSysMenu.SmLevel
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbSysMenu dbSysMenu)
		{	
			string sql = @"Update DbSysMenu set 

					SmName= '{0}',
					SmIcon= '{1}',
					SmCode= '{2}',
					SmUrl= '{3}',
					SmStatus= {4},
					SmUpCode= '{5}',
					SmOrder= {6},
					SmLevel= {7}
				where

					SmId= {8}";
			sql = string.Format(sql,
					SqlHelper.String(dbSysMenu.SmName),
					SqlHelper.String(dbSysMenu.SmIcon),
					SqlHelper.String(dbSysMenu.SmCode),
					SqlHelper.String(dbSysMenu.SmUrl),
					dbSysMenu.SmStatus,
					SqlHelper.String(dbSysMenu.SmUpCode),
					dbSysMenu.SmOrder,
					dbSysMenu.SmLevel,
					dbSysMenu.SmId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 smId)
		{
		
			string sql = @"Delete from DbSysMenu where 
					SmId= {0}";
			sql = string.Format(sql,
					smId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbSysMenu a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbSysMenu Select(Int32 smId)
		{
			DbSysMenu dbSysMenu = null;
			string sql = @"select a.* from DbSysMenu a  where
					a.SmId= {0}
";
			sql = string.Format(sql,
					smId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbSysMenu = new DbSysMenu(ds.Tables[0].Rows[0]);					
			return dbSysMenu;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbSysMenu a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbSysMenu a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbSysMenu a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
